import java.util.List;

public class MySingleList implements IList{

    static  class  ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val){
            this.val=val;
        }

    }

    public ListNode head;

    @Override
    public void addFirst(int data) {
        ListNode node=new ListNode(data);

        node.next=head;
        head=node;
    }

    @Override
    public void addLast(int data) {
        ListNode node =new ListNode(data);

        if(head==null){
            head=node;
            return;
        }
        ListNode cur=head;
        while(cur.next!=null){
            cur=cur.next;
        }
        cur.next=node;
    }

    @Override
    public void addIndex(int index, int data) {
        try{
            checkIndex(index);
            if (index==0){
                addFirst(data);
                return;
            }
            if(index==size()){
                addLast(data);
                return;
            }
            ListNode cur=head;
            while(index-1!=0){
                index--;
                cur=cur.next;
            }
            ListNode node=new ListNode(data);
            node.next=cur.next;
            cur.next=node;
        }catch (IndexisTrue e){
            e.printStackTrace();
        }
    }

    private void checkIndex(int Index) throws IndexisTrue{
        if(Index<0||Index>size()){
            throw new IndexisTrue("Pos位置不合法");
        }
    }

    @Override
    public boolean contains(int key) {
        ListNode cur=head;
        while (cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if (head==null){
            return;
        }
        if (head.val==key){
            head=head.next;
            return;
        }
        ListNode cur=findNodeOfKey(key);
        if (cur==null){
            return;
        }
        ListNode del=cur.next;
        cur.next=del.next;
    }

    private ListNode findNodeOfKey(int key){
        ListNode cur=head;
        while (cur.next!=null){
            if(cur.next.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }

    @Override
    public void removeAllkey(int key) {
        if(head==null){
            return;
        }
        ListNode prev=head;
        ListNode cur=head.next;
        while (cur!=null){
            if(cur.val==key){
                prev.next=cur.next;
                cur=cur.next;
            }else{
                prev=cur;
                cur=cur.next;
            }
        }
        if (head.val==key){
            head=head.next;
        }
    }

    @Override
    public int size() {
        int len=0;
        ListNode cur=head;
        while (cur!=null){
            len++;
            cur=cur.next;
        }

        return len;
    }

    @Override
    public void clear() {
        ListNode cur=head;
        while (cur!=null){
            ListNode curN=cur.next;
            cur.next=null;
            cur=curN;
        }
        head=null;
    }

    @Override
    public void display() {
        ListNode cur=head;
        while (cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }

    //链表中间的节点,如果是偶数则返回第二个
    public ListNode middleNode(){
        if (head==null){
            return head;
        }
        //快慢对象
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null && fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        return slow;
    }

    //找倒数第K个节点
    public int  kthToLast(int k){
        if(head==null)
            return -1;
        if (k<0)
            return -1;
        //如果k超过整个链表的长度
//        if(k>size()){//这样写不好，还得求链表的长度
//            return -1;
//        }
        ListNode fast=head;
        ListNode slow=head;
        int count=0;
        while(count!=k-1){
            fast=fast.next;
            if(fast==null){
                return -1;
            }
            count++;
        }
        while (fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        return slow.val;
    }
}
